﻿using FinalPointSet.Model.Util;
using System.Collections.Generic;
using System.Linq;

namespace FinalPointSet.Model.Model.Knowledge
{
    public static class KnowledgePointHelper
    {
        /// <summary>
        /// 获取关键词匹配的知识点
        /// </summary>
        /// <param name="knowledges"></param>
        /// <param name="keywordMatchWorth"></param>
        /// <param name="searchWord"></param>
        /// <returns></returns>
        public static List<KnowledgePoint> GetMatchKnowledgePoints(List<KnowledgePoint>? knowledges, KeywordMatchWorth? keywordMatchWorth, string searchWord)
        {
            if (knowledges == null || keywordMatchWorth == null)
                return [];
            List<KnowledgePoint> searchResults = knowledges.Where((knowledge) =>
            {
                string keyword = searchWord.ToLower();
                //1.完全匹配
                if (keyword.Equals(knowledge.Name))
                {
                    knowledge.Weight = keywordMatchWorth.TotalMatchValue;
                    return true;
                }
                //2.部分匹配名称
                if (MethodUtil.IsMatch(keyword, knowledge.Name))
                {
                    knowledge.Weight = keywordMatchWorth.NameMatchValue;
                    return true;
                }
                else if (MethodUtil.GetSimilarityRatio(keyword, knowledge.Name) >= keywordMatchWorth.FuzzyMatchRate)
                {
                    knowledge.Weight = keywordMatchWorth.FuzzyMatchRate;
                    return true;
                }
                //3.部分匹配标签
                if (MethodUtil.IsMatch(keyword, knowledge.Keyword))
                {
                    string[] tags = knowledge.Keyword.Split(',');
                    //统计匹配的标签数量
                    int cnt = 0;
                    foreach (string tag in tags)
                    {
                        if (MethodUtil.IsMatch(tag, keyword))
                        {
                            cnt++;
                        }
                    }
                    knowledge.Weight = keywordMatchWorth.KeywordMatchValue * cnt;
                    return true;
                }
                else if (MethodUtil.GetSimilarityRatio(keyword, knowledge.Name) >= keywordMatchWorth.FuzzyMatchRate)
                {
                    knowledge.Weight = keywordMatchWorth.FuzzyMatchValue;
                    return true;
                }
                //4.匹配语言
                if (MethodUtil.IsMatch(keyword, knowledge.Language.Name))
                {
                    knowledge.Weight = keywordMatchWorth.LanguageMatchValue;
                    return true;
                }
                return false;
            }).ToList();

            //排序
            if (searchResults != null && searchResults.Count != 0)
            {
                searchResults.Sort((kpo1, kpo2) => (int)(kpo2.Weight - kpo1.Weight));
            }
            else
            {
                searchResults = [];
            }

            return searchResults;
        }

        public static List<KnowledgePoint> Update(List<KnowledgePoint> origns, List<KnowledgePoint> news)
        {
            List<KnowledgePoint> results = new(origns);
            foreach (KnowledgePoint item in news)
            {
                KnowledgePoint? kp = news.FirstOrDefault(kp => kp.ID == item.ID);
                if (kp != null)
                {
                    item.ID = kp.ID;
                    item.Name = kp.Name;
                    item.Language = kp.Language;
                    item.Keyword = kp.Keyword;
                    item.Content = kp.Content;
                    item.Weight = kp.Weight;
                }
            }
            return results;
        }
    }
}
